<?php
class App_Tpl_Form extends Mozg_Templator
{
	protected $_form;
	protected $_showDefined = false;

	protected $_isMultilang = false;

	public function Form(Mozg_Property_Params $obj)
	{
		return $obj->Form($this->_form);
	}

	/*
	 * TODO: сделать постинг только из отдельных групп.
	 */
    protected function PreparePostData(Mozg_Property_Params $obj, array $data)
    {
    	$filteredData = array();
    	foreach ($data as $groupName => $groupData)
    	{
    		$groupFields = $this->Form($obj)->GetGroupEditableFields($groupName);
			foreach ($groupData as $fieldKey => $fieldValue)
			{
				if (!in_array($fieldKey, $groupFields)) continue;

		    	$params = $this->Form($obj)->GetFieldConfig($groupName, $fieldKey);

				$fieldTpl = $this->GetFieldTpl($params['type']);

				if ($obj->Config()->IsMultilangField($fieldKey))
				{
					if (is_array($fieldValue))
					{
						foreach (Mozg_Lang::$langs as $lang)
						{
							if (array_key_exists($lang, $fieldValue))
							{
								$fieldId = $this->Form($obj)->GetUniqueId($groupName, $fieldKey, $lang);
								$currentVal = $obj->{'Get' .$fieldKey}($lang);
								$filteredData[$fieldKey][$lang] = $fieldTpl->PreparePost($fieldValue[$lang], $params, $currentVal, $fieldId);
							}
						}
					}
				}
				else
				{
					$fieldId = $this->Form($obj)->GetUniqueId($groupName, $fieldKey);
					$currentVal = $obj->{'Get' .$fieldKey}($lang);
					$filteredData[$fieldKey] = $fieldTpl->PreparePost($fieldValue, $params, $currentVal, $fieldId);
				}
			}
    	}

    	return $filteredData;
    }

    public function SetPostData(Mozg_Property_Params $obj, array $data)
    {
    	$obj->ClearData($this->Form($obj)->GetEditableFields());
    	$obj->AddData($this->PreparePostData($obj, $data));
    }

    public function AddPostData(Mozg_Property_Params $obj, array $data)
    {
    	$obj->AddData($this->PreparePostData($obj, $data));
    }

	public function GetFieldTpl($fieldType)
	{
		return $this->GetTpl('form_field_' .$fieldType);
	}

    public function WrappedField($groupName, $fieldName, Mozg_Property_Params $obj)
    {
    	if (!in_array($fieldName, $this->Form($obj)->GetGroupFields($groupName))) return false;

    	$this->params = $this->Form($obj)->GetFieldConfig($groupName, $fieldName);

		if ($obj->Config()->IsMultilangField($fieldName))
    	{
    		$this->_isMultilang = true;
        	$langs = Mozg_Lang::$langs;
    	}
    	else
    	{
    		$langs = array(null);
    	}

    	$this->varName = $this->Form($obj)->GetVar();

    	$html = '';

		foreach ($langs as $lang)
    	{
    		$this->lang = $lang;
    		$this->fieldId = $this->Form($obj)->GetUniqueId($groupName, $fieldName, $lang);
    		$this->fieldName = $this->Form($obj)->GetFullName($groupName, $fieldName, $lang);
    		$this->origFieldName = $fieldName;
    		$this->groupName = $groupName;
    		$this->error = $obj->Validator()->GetError($fieldName, $lang);

    		if ($this->_showDefined && !$obj->HasSet($fieldName, $lang))
    		{
    			$this->fieldHTML = false;
    		}
    		else
    		{
    			$this->fieldHTML = $this->GetFieldInput($groupName, $fieldName, $obj, $lang);
    		}

	    	$html .= $this->RenderFieldWrapper();
    	}

    	return $html;
    }

    public function GetFieldInput($groupName, $fieldName, Mozg_Property_Params $obj, $lang)
    {
    	if (!in_array($fieldName, $this->Form($obj)->GetGroupFields($groupName))) return false;

    	$this->params = $this->Form($obj)->GetFieldConfig($groupName, $fieldName);

    	$this->lang = $lang;
    	$this->fieldId = $this->Form($obj)->GetUniqueId($groupName, $fieldName, $lang);
    	$this->fieldName = $this->Form($obj)->GetFullName($groupName, $fieldName, $lang);
    	$this->origFieldName = $fieldName;
    	$this->groupName = $groupName;
    	$this->error = $obj->Validator()->GetError($fieldName, $lang);

    	$fieldTpl = $this->GetFieldTpl($this->params['type']);

    	$fieldValue = (null === $lang) ? $obj->{'Get' .$fieldName}() : $obj->{'Get' .$fieldName}($lang);
    	$this->fieldValueHTML = $fieldTpl->GetField($this->fieldId, $this->fieldName, $fieldValue, $this->params, $this->error);

    	return $this->RenderFieldInput();
    }

    protected function RenderFieldInput()
    {
    	return $this->render('form/block/_fieldvalue.phtml');
    }

    protected function RenderFieldWrapper()
    {
    	return $this->render('form/block/_fieldwrapper.phtml');
    }

    public function GetBlock(Mozg_Property_Params $obj)
    {
		$groups = $this->Form($obj)->GetGroups();

		$htmlGroups = array();
		foreach ($groups as $groupName)
		{
			if ($htmlGroup = $this->GetGroup($groupName, $obj))
			{
				$htmlGroups[$groupName] = $htmlGroup;
			}
		}

        $this->htmlGroups = $htmlGroups;
        $this->fieldConfig = $obj->Config();
        $this->varName = $this->Form($obj)->GetVar();

        return $this->RenderBlock();
    }

    public function GetGroup($groupName, Mozg_Property_Params $obj)
    {
		$fields = $this->Form($obj)->GetGroupFields($groupName);

		if (!sizeof($fields)) return false;

		$this->firstChild = true;
		$this->lastChild = false;

		$htmlFields = array();
		$i = 0;
		$total = sizeof($fields);
		foreach ($fields as $fieldName)
		{
			$i++;
			if ($i == $total)
			{
				$this->lastChild = true;
			}

			$htmlFields[$fieldName] = $this->WrappedField($groupName, $fieldName, $obj);

			$this->firstChild = $this->lastChild = false;
		}

        $this->htmlFields = $htmlFields;
        $this->groupName = $groupName;
        $this->groupTitle = $this->Form($obj)->GetGroupTitle($groupName);
		$this->groupParams = $this->Form($obj)->GetGroupParams($groupName);
		$this->varName = $this->Form($obj)->GetVar();

        return $this->RenderGroup();
    }

    protected function RenderGroup()
    {
    	return $this->render('form/block/_group.phtml');
    }

    protected function RenderBlock()
    {
    	return $this->render('form/block/_block.phtml');
    }

    public function RenderJS()
    {
        return $this->render('form/block/js/_fields_js.js');
    }

	protected function GetNewFieldUrl()
	{
		return '';
	}

	public function IsMultilang()
    {
    	return $this->_isMultilang;
    }

}